<?php

namespace common\components\system;

use Yii;
use yii\db\BaseActiveRecord;

/**
 * 系统日志记录器
 *
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class Loger {

    /**
     * 默认监听事件
     * @return array BaseActiveRecord's Events.
     */
    protected static function listens() {
        return [
            BaseActiveRecord::EVENT_AFTER_FIND,
            BaseActiveRecord::EVENT_AFTER_INSERT,
            BaseActiveRecord::EVENT_AFTER_UPDATE,
            BaseActiveRecord::EVENT_AFTER_DELETE,
        ];
    }

    /**
     * 记录日志
     * @final
     */
    public static function record($event) {
        $classname = BaseActiveRecord::className();
        foreach (self::listens() as $e) {
            $params = [
                $classname,
                $e,
                [
                    'common\models\AdminLog',
                    $e
                ]
            ];
            call_user_func_array(['\yii\base\Event', 'on'], $params);
        }
    }

}
